package org.littlesun.management.ui.composite.user;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.littlesun.management.model.user.User;
import org.littlesun.management.model.user.User.SimpleUser;

public class EditUserDialog extends Dialog {
	
	@Getter @Setter private boolean editable;
	@Getter private User user;

	private Text nameText;
	private Text idText;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public EditUserDialog(Shell parentShell, User user) {
		super(parentShell);
		this.user = user == null? new SimpleUser() : new SimpleUser(user);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 4;

		Label idLabel = new Label(container, SWT.NONE);
		idLabel.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		idLabel.setText("\u59D3\u540D:");
		GridData gd_idLabel = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_idLabel.widthHint = 100;
		idLabel.setLayoutData(gd_idLabel);
		
		idText = new Text(container, SWT.BORDER);
		idText.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		idText.setEditable(this.editable);
		idText.setText(user.getName() == null? "" : user.getName());
		GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		gd_idText.widthHint = 200;
		idText.setLayoutData(gd_idText);
		
		Label nameLabel = new Label(container, SWT.NONE);
		nameLabel.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		nameLabel.setText("\u59D3\u540D:");
		GridData gd_nameLabel = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_nameLabel.widthHint = 100;
		nameLabel.setLayoutData(gd_nameLabel);
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setFont(SWTResourceManager.getFont("Microsoft JhengHei UI", 12, SWT.NORMAL));
		nameText.setEditable(this.editable);
		nameText.setText(user.getName() == null? "" : user.getName());
		GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		gd_nameText.widthHint = 200;
		nameText.setLayoutData(gd_nameText);

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

}
